package com.gitee.candy.number;

/**
 * @author ：suyanlong
 * @description：
 * @version: 1.0
 * 10进制与64进制互转
 */
public class ShotChain {

    /**
     * 在进制表示中的字符集合
     */
    final static String v64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";

    // 10进制 --> 64进制
    public static String toOtherString(long n) {
        long num = n;
        char[] buf = new char[1 << 6];
        int charPos = 1 << 6;
        int base = 1 << 6;
        while (num >= base) {
            buf[--charPos] = v64.charAt((int) (num & (~-base)));
            num >>= 6;
        }
        buf[--charPos] = v64.charAt((int) (num & (~-base)));
        return new String(buf, charPos, ((~-base) ^ (~-charPos)));
    }

    // 64进制 --> 10进制
    public static long therString(String v16) {
        long result = 0;
        for (char c : v16.toCharArray()) {
            result = result == 0 ? v64.lastIndexOf(c) : ((result << 6) | v64.lastIndexOf(c));
        }
        return result;
    }
}
